<template>
  <section>
    <!--工具条-->
    <!-- <div>{{winWidth}}</div>
    <div>{{winHeight}}</div> -->
    <!-- <el-col :xs="6" :sm="6" :md="6" :lg="6" style="height:670px;width:20%;overflow-y :auto;overflow-x :auto">
      <el-tree highlight-current :data="treedata" :props="defaultProps" @node-click="handleNodeClick" :default-expand-all="true" :expand-on-click-node="true">
      </el-tree>
    </el-col> -->
    <div style="display:flex;justify-content:space-between;width:100%;height:100%">
    <div style="width:20%;position:relative;">
      <div  style="position:absolute;width:100%;height:100%;overflow-y:auto;overflow-x:auto">
         <el-col  style="overflow:hidden;width:auto" >
        <el-tree  highlight-current :data="treedata" :props="defaultProps" @node-click="handleNodeClick" :default-expand-all="true" :expand-on-click-node="true">
        </el-tree>
      </el-col>
      </div>
    </div>
   <div style="width:80%;">
    <el-col >
    <el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
      <el-form :inline="true" :model="filters">
        <el-form-item>
          <el-input size="small" style="width:170px;" v-model.trim="filters.userCode" placeholder="请输入用户编码"></el-input>
        </el-form-item>
        <el-form-item>
          <el-input size="small" style="width:180px;" v-model.trim="filters.mobile" placeholder="请输入用户手机"></el-input>
        </el-form-item>
        <el-form-item>
          <el-input size="small" style="width:180px;" v-model.trim="filters.policyNo" placeholder="请输入保单号"></el-input>
        </el-form-item>
        <el-form-item>
          <el-tooltip class="item" effect="dark" content="消费时间" placement="top">
            <el-date-picker size="small" style="width:180px;" v-model.trim="filters.Time" type="daterange" align="left" placeholder="请选择消费时间范围">
            </el-date-picker>
          </el-tooltip>
        </el-form-item>
        <el-form-item>
          <el-select size="small" clearable style="width:180px;" v-model.trim="filters.status" placeholder="请选择消费类型">
            <el-option label="活动" value="1"></el-option>
            <el-option label="规则" value="2"></el-option>
            <el-option label="手工" value="3"></el-option>
            <el-option label="兑换" value="4"></el-option>
            <el-option label="退保" value="5"></el-option>
            <el-option label="过期" value="6"></el-option>
            <el-option label="商城退还" value="7"></el-option>
            <el-option label="退保差额" value="8"></el-option>
            <el-option label="提现" value="9"></el-option>
            <el-option label="扣减差额" value="10"></el-option>
            <el-option label="好友签单退保" value="14"></el-option>
            <el-option label="间接好友签单退保" value="15"></el-option>
            <el-option label="渠道退保" value="16"></el-option>
          </el-select>
        </el-form-item>

        <el-form-item style="float:right;">
          <el-button size="small" type="primary" icon="upload2" v-show="useExport" @click="exportExcels">导出</el-button>
        </el-form-item>
        <el-form-item style="float:right;">
          <el-button size="small" type="primary" v-show="useQuery" icon="search" v-on:click="getLogs(true)">查询</el-button>
        </el-form-item>
      </el-form>
    </el-col>

    <!--列表-->
    <el-table :data="Logs" stripe border :height="winHeight-188" highlight-current-row v-loading="listLoading" @selection-change="selsChange" style="width: 100%;">
      <el-table-column prop="id" align="center" label="ID" width="80" show-overflow-tooltip>
      </el-table-column>
      <el-table-column prop="granter" align="center" label="出库人" width="120" show-overflow-tooltip>
      </el-table-column>
      <el-table-column prop="userCode" align="center" label="客户" min-width="140" show-overflow-tooltip>
      </el-table-column>
      <el-table-column prop="telephone" align="center" label="客户手机号" min-width="140" show-overflow-tooltip>
      </el-table-column>
      <el-table-column prop="reason" align="center" label="扣减原因" min-width="150" show-overflow-tooltip>
      </el-table-column>
      <el-table-column prop="reChangeScore" align="center" label="应扣减积分" width="120" :formatter="formatreChangeScore">
      </el-table-column>
      <el-table-column prop="acChangeScore" align="center" label="实扣减积分" width="120" :formatter="formatacChangeScore">
      </el-table-column>

      <el-table-column prop="changeTime" align="center" label="扣减时间" min-width="130" show-overflow-tooltip>
      </el-table-column>
      <el-table-column prop="reasonNo" align="center" label="原因单号" min-width="120" show-overflow-tooltip>
      </el-table-column>
      <el-table-column prop="changeType" align="center" label="扣减类型" width="150" :formatter="formatStatus">
      </el-table-column>
      <el-table-column prop="effictiveDate" align="center" label="生效时间" min-width="130" show-overflow-tooltip>
      </el-table-column>
      <el-table-column prop="invalidDate" align="center" label="截止时间" min-width="130" show-overflow-tooltip>
      </el-table-column>
      <!-- <el-table-column label="操作" fixed='right' width="100">
        <template scope="scope">
          <el-button size="small" @click="handleEdit(scope.$index, scope.row)">修改</el-button>
          <el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
        </template>
      </el-table-column> -->
    </el-table>

    <!--工具条-->
    <el-col :span="24" class="toolbar">
      <!-- <el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button> -->

      <el-pagination style="float:right" @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="page" :page-sizes="[10, 20, 40, 80]" :page-size="pageSize" layout="total, sizes, prev, pager, next, jumper" :total="total">
      </el-pagination>
    </el-col>
    </el-col>
    </div>
    </div>
  </section>
</template>

<script>
import util from '../../common/js/util'

import commons from '../../common/js/common'
//import NProgress from 'nprogress'
import { mapState, mapActions } from 'vuex'
import { outScoreDetail,treeNextChannelList,orderAuth } from '../../api/api';

export default {
  data() {
    return {
      filters: {
        policyNo:'',
        userCode: '',
        mobile: '',
        Time: '',
        status: ''
      },
      Logs: [],
      lists: [],
      exportTotal: 0,
      total: 0,
      page: 1,
      pageSize: 10,
      treedata: [],
      defaultProps: {
        children: 'children',
        label: 'label'
      },
      loading: '',
      // fullscreenLoading:false,
      listLoading: false,
      addLoading: false,
      addFormVisible: false,
      useExport: false,
      useQuery: false,
      comCode:'',
      sels: [],//列表选中列
      userid: '',
      addFormRules: {

      },
      IsShow: false
    }
  },
  computed: mapState([
    'winWidth',
    'winHeight'
  ]),
  methods: {
    cellStyle(row, rowIndex, columnIndex) {
    },
    formatEffictiveDate: function (row, column) {
      return commons.getDaytime(row.effictiveDate, 1, false);
    },
    formatInvalidDate: function (row, column) {
      return commons.getDaytime(row.invalidDate, 1, false);
    },
    formatStatus: function (row, column) {
      return commons.consumeStatus(row.changeType);
    },
    formatTime: function (row, column) {
      return commons.getDaytime(row.createTime, 1, false);
    },
    formatacChangeScore: function (row, column) {
      return commons.abs(row.acChangeScore);
    },
    formatreChangeScore: function (row, column) {
      return commons.abs(row.reChangeScore);
    },
    handleCurrentChange(val) {
      this.page = val;
      this.getLogs(true);
    },
    handleSizeChange(val) {
      this.pageSize = val;
      this.getLogs(true);
    },
    //获取列表
    getLogs(bool) {
      let para = new FormData();
      para.append("pageSize", bool ? this.pageSize : this.exportTotal);
      para.append("pageNumber", bool ? this.page : 1);
      para.append("changeType", this.filters.status);
      para.append("comCode", commons.isEmpty(this.comCode)?"00000000":this.comCode);
      // para.append("comCode", this.comCode);
      para.append("startDate", commons.changeTimes(this.filters.Time, 'start'));
      para.append("endDate", commons.changeTimes(this.filters.Time, 'end'));
      para.append("policyNo", this.filters.policyNo);
      para.append("userCode", this.filters.userCode);
      para.append("mobile", this.filters.mobile);
      this.listLoading = bool;
      // this.fullscreenLoading = bool?false:true;
      if (!bool) {
        this.loading = this.$loading({
          lock: true,
          text: '正在导出',
          spinner: 'el-icon-loading',
          background: 'rgba(0, 0, 0, 0.7)'
        });
      }
      outScoreDetail(para).then((res) => {
        this.lists = res.data.result.content;
        this.total = res.data.result.totalElements;
        this.exportTotal = res.data.result.totalElements;
        this.listLoading = false;
        if (!bool) {
          this.loading.close();
          this.export2Excel();
        }
        else {
          this.Logs = res.data.result.content;
        }
      });
    },
    handleNodeClick: function (data, node, obj) {
      this.comCode = data.id;
      let para = new FormData();
      para.append('branchNo', data.id);
      para.append('type', 'order');
      treeNextChannelList(para).then((res) => {
        if (res.data.result && res.data.success == true) {
          for (let i in res.data.result) {
            const newChild = { id: res.data.result[i].userCode, label: res.data.result[i].shopName, children: [] };
            if (!data.children) {
              this.$set(data, 'children', []);
            }
            if (typeof (commons.GetByID(this.treedata, res.data.result[i].userCode)) == 'undefined') {
              data.children.push(newChild);
            }
          }
        }
      });
      this.getLogs(true);
    },

    getSelectPerTree: function () {
      let para = new FormData();
      var userinfo = sessionStorage.getItem('user');
      if (userinfo) {
        var user = JSON.parse(userinfo);
      }
      var userid = user.userId;
      para.append('userId', userid);
      para.append('type', 'order');
      let modelTree = this.treedata;
      let leafTree = [];
      orderAuth(para).then((res) => {
        var data = res.data.result;
        if (data) {
          this.comCode=data[0].branchNo;
        }
        gettree(data, this.treedata);
        console.log(this.treedata)
      });
      function gettree(arr, trees) {
        for (var i in arr) {
          if (typeof (commons.GetByID(trees, arr[i].branchNo)) == 'undefined') {
            trees.push({
              id: arr[i].branchNo,
              label: arr[i].branchCname,
              children: []
            });
          }
          if (arr[i].nextBranch) {
            let childTree = commons.GetByID(trees, arr[i].branchNo);
            gettree(arr[i].nextBranch, childTree.children);
          }
        }
      }
       // setTimeout(() => {
        this.getLogs(true);
      // }, 1000);
    },
    //table导出excel
    exportExcels: function () {
      this.getLogs(false);
    },
    export2Excel: function () {
      require.ensure([], () => {
        const { export_json_to_excel } = require('../../vendor/Export2Excel');
        const tHeader = ['ID', '实扣减积分', '应扣减积分', '扣减时间', '扣减类型', '生效时间', '截止时间', '使用人', '客户', '客户手机号', '扣减原因'];
        const filterVal = ['id', 'acChangeScore', 'reChangeScore', 'changeTime', 'changeType', 'effictiveDate', 'invalidDate', 'granter', 'userCode', 'telephone', 'reason'];
        const list = this.lists;
        for (var i in list) {
          // list[i].effictiveDate = commons.getDaytime(list[i].effictiveDate, 1, false);
          // list[i].invalidDate = commons.getDaytime(list[i].invalidDate, 1, false);
          list[i].changeType = commons.consumeStatus(list[i].changeType);
        }
        const data = this.formatJson(filterVal, list);
        export_json_to_excel(tHeader, data, '使用明细列表');
      })
    },
    formatJson: function (filterVal, jsonData) {
      return jsonData.map(v => filterVal.map(j => v[j]))
    },
    selsChange: function (sels) {
      this.sels = sels;
    },
    btnShow: function () {
      let thisData = JSON.parse(sessionStorage.getItem('174'));
      let _this = this;
      $.each(thisData, function (i, j) {
        if (thisData[i] == "useExport") {
          _this.useExport = true;
        }
        if (thisData[i] == "useQuery") {
          _this.useQuery = true;
        }
      });
    },
  },
  mounted() {
    this.btnShow();
    this.getSelectPerTree();
    // this.getLogs(true);
  }
}

</script>

<style scoped>
  .el-tree {
    border: 0;
  }
</style>